package com.xyh.system.controller;


import com.xyh.common.util.ResultCode;
import com.xyh.common.util.ResultVO;
import com.xyh.system.model.Dept;
import com.xyh.system.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyle
 * @since 2021-01-05
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @GetMapping("/index")
    public String toDeptIndex(){
        return "dept/dept-list";
    }

    /**
     *
     * @param dept
     * @return
     */
    @PostMapping
    @ResponseBody
    public ResultVO add(Dept dept) {
        boolean b = deptService.insert(dept);
        if (b) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }


    /**
     * 获取部门树
     *
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public ResultVO deptTree() {
        ResultVO success = ResultVO.success(deptService.selectAllDeptTree());
        System.out.println("success = " + success);
        return success;
    }


    /**
     * 根据父级id获取该菜单下的所有子集菜单
     *
     * @param parentId
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultVO getChildDeptByParentId(@RequestParam(required = false) Integer parentId) {
        List<Dept> childDepts = deptService.selectChildDeptByParentId(parentId);

        return ResultVO.success(childDepts);
    }


}
